﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;

namespace ProjectRoughWork
{
    class ScreenManager : IScreenManager
    {
        /// <summary>
        /// Default Constructor. Initializes the screen list;
        /// </summary>

        private ScreenManager()
        {
            screenStack = new List<IScreen>();
            tempList = new List<IScreen>();
        }

        /// <summary>
        /// Returns an instance of the ScreenManager, initializing the Screen Manager if necessary
        /// </summary>
        /// <returns></returns>

        public static IScreenManager GetInstance()
        {
            if (pInstance == null)
            {
                pInstance = new ScreenManager();
            }
            return pInstance;
        }

        /// <summary>
        /// Adds screen to the top of the stack, updating old top of the stack appropriatelyit was a pop up 
        /// </summary>
        /// <param name="newScreen"></param>

        public void AddScreen(IScreen newScreen)
        {
            if (    (!newScreen.isPopUp()) && 
                    (screenStack.Count > 0) 
                )
            {
                screenStack[screenStack.Count - 1].Deactivate();
            }

            screenStack.Add(newScreen);
        }

        /// <summary>
        /// Remove screen at the top of the stack
        /// </summary>

        public void popScreen()
        {
            if (screenStack.Count > 0)
            {
                screenStack.Remove(screenStack[screenStack.Count - 1]);
            }
        }

        public IScreen getTop()
        {
            if (screenStack.Count > 0)
            {
                return screenStack[screenStack.Count - 1];
            }
            return null;
        }

        /// <summary>
        /// Passes Keyboard input to the screen at the top of the stack
        /// </summary>
        /// <param name="inputKey"></param>

        public void HandleInput(KeyboardState inputKey)
        {
            if (screenStack.Count > 0)
            {
                screenStack[screenStack.Count - 1].HandleInput(inputKey);
            }
        }
        
        /// <summary>
        /// Passes Mouse input to the screen at the top of the stack
        /// </summary>
        /// <param name="inputClick"></param>

        public void HandleInput(MouseState inputClick)
        {
            if (screenStack.Count > 0)
            {
                screenStack[screenStack.Count - 1].HandleInput(inputClick);
            }
        }

        public void HandleInput(GamePadState inputButton)
        {
            if (screenStack.Count > 0)
            {
                screenStack[screenStack.Count - 1].HandleInput(inputButton);
            }
        }

        /// <summary>
        /// Updates the screen at the top of the stack
        /// </summary>

        public void Update()
        {
            if (screenStack.Count > 0)
            {
                screenStack[screenStack.Count - 1].Update();
            }
        }

        /// <summary>
        /// Draws the active screens
        /// </summary>

        public void Draw()
        {
            foreach (IScreen currentScreen in screenStack)
            {
                if (currentScreen.isActive())
                {
                    currentScreen.Draw();
                }
            }
        }

        //Data Members

        List<IScreen> screenStack;          //Stack of Screens
        List<IScreen> tempList; //Do I still need this?

        static ScreenManager pInstance;     //Instance of the screen manager
    }
}
